<?php

namespace app\admin\controller\zhongzhuan\feiyu;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Feiyutoec extends Backend
{

    /**
     * Feiyutoec模型对象
     * @var \app\admin\model\zhongzhuan\feiyu\Feiyutoec
     */
    protected $model = null;
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\zhongzhuan\feiyu\Feiyutoec;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $ids = $this->request->param('ids',0);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with([
                    'userinfo'=>function($query){
                        $query->withField('userid,name,account,mobile');
                    }

                ])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
                
     


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }        
        return $this->view->fetch();
    }
    
    
    public function tuisongdetail($ids){
        $row = $this->model->get($ids);
        $content_list = json_decode($row['content'],true);
        $config = [
            'followUserId'=>'跟进人','mobile'=>'客户手机号','name'=>'名称','fields'=>'信息',
        ];
        $end_content_list = [];
        
        foreach($content_list as $key =>$value){
            $end_content_list[$config[$key]] = is_array($value) ? json_encode($value): $value;
        }
        
        $this->view->assign("end_content_list", $end_content_list);
        return $this->view->fetch();
    }
    
    
    public function indexselectpage()
    {
        
        $prarms = $this->request->request();

        $page = $prarms["pageNumber"]?:1;
        $size = $prarms["pageSize"]?:10;

        $where = array();
        //$where['department_id'] = 3;//3 专家部门
        $prarms['zidingyi'] && $where["jobName|callJobId"] = array("like","%".$prarms['zidingyi']."%");
        !empty($prarms['searchKey']) && $where[$prarms['searchKey']] = $prarms['searchValue'];
        if(!empty($prarms['searchValue'])) {
            $where['id'] = ['in',$prarms['searchValue']];
        }
        $where2 = [];
        

        $count = Db::name('ec_ai_task')
            ->where($where)
            ->where($where2)
            ->count();

        $lists = Db::name('ec_ai_task')
            ->limit(($page-1)*$size,$size)
            ->field("id,callJobId,jobName")
            ->order("id desc")
            ->where($where)
            ->where($where2)
            ->select();
        foreach($lists as $k=>$v){
            $lists[$k]["zidingyi"] = $v["jobName"].' '.$v["callJobId"];
        }

        $result = array("total" => $count, "rows" => $lists);
        return json($result);
    }


}
